package craky.keeper.category;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.border.EmptyBorder;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import craky.component.JImagePane;
import craky.componentc.JCButton;
import craky.componentc.JCDialog;
import craky.componentc.JCLabel;
import craky.componentc.JCMessageBox;
import craky.componentc.JCStringField;
import craky.keeper.EmptyComponent;
import craky.keeper.KeeperApp;
import craky.layout.LineLayout;
import craky.util.UIUtil;

public class CategoryInfoDialog extends JCDialog implements ActionListener {
	private static final long serialVersionUID = -538668122701322794L;
	private JComponent content;
	private JCButton btnOk;
	private JCButton btnCancel;
	private JCStringField txtName;
	private Category category;
	private boolean isAdd;

	public CategoryInfoDialog(KeeperApp keeper, Window owner, Category category) {
		super(owner, category == null ? "添加消费类别" : "修改消费类别",
				Dialog.ModalityType.DOCUMENT_MODAL);
		this.category = category;
		this.isAdd = (category == null);
		initUI();
		setVisible(true);
	}

	private void initUI() {
		this.content = ((JComponent) getContentPane());
		JImagePane mainPane = new JImagePane();
		EmptyComponent ecName = new EmptyComponent();
		JImagePane buttonPane = new JImagePane();
		JCLabel lbName = new JCLabel("名称");
		this.txtName = new JCStringField(25);
		this.btnOk = new JCButton("确定");
		this.btnCancel = new JCButton("取消");

		this.txtName.setText(this.isAdd ? null : this.category.getName());
		this.content.setBorder(new EmptyBorder(0, 2, 2, 2));
		this.content.setPreferredSize(new Dimension(230, 120));
		this.content.setLayout(new BorderLayout());
		mainPane.setLayout(new LineLayout(8, 15, 15, 15, 0, 10, 10, 1));
		mainPane.setBackground(new Color(255, 255, 255, 210));
		ecName.setLayout(new BorderLayout());
		ecName.setPreferredSize(new Dimension(-1, 22));
		buttonPane.setLayout(new LineLayout(5, 7, 7, 7, 0, 10, 10, 0));
		buttonPane.setBackground(new Color(255, 255, 255, 150));
		buttonPane.setPreferredSize(new Dimension(-1, 32));
		lbName.setPreferredSize(new Dimension(40, -1));
		this.btnOk.setPreferredSize(new Dimension(69, 21));
		this.btnOk.addActionListener(this);
		this.btnCancel.setPreferredSize(this.btnOk.getPreferredSize());
		this.btnCancel.addActionListener(this);
		ecName.add(lbName, "West");
		ecName.add(this.txtName, "Center");
		buttonPane.add(this.btnOk, "End");
		buttonPane.add(this.btnCancel, "End");
		mainPane.add(ecName, "StartFill");
		this.content.add(buttonPane, "South");
		this.content.add(mainPane, "Center");
		setDefaultCloseOperation(0);
		pack();
		setResizable(false);
		setLocationRelativeTo(getOwner());
		UIUtil.escAndEnterAction(this, this.btnOk, new AbstractAction() {
			private static final long serialVersionUID = -933736543655670099L;

			public void actionPerformed(ActionEvent e) {
				CategoryInfoDialog.this.cancel();
			}
		});
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				CategoryInfoDialog.this.cancel();
			}
		});
	}

	private boolean exist(String name) {
		return !CategoryDAO.getCategorys(
				new Criterion[] { Restrictions.eq("name", name) }).isEmpty();
	}

	private void submit() {
		String name = this.txtName.getText();
		String retInfo = null;
		boolean success = false;

		if ((name == null) || ((name = name.trim()).isEmpty())) {
			retInfo = "名称不能为空";
		} else if (exist(name)) {
			retInfo = "该消费类别已存在";
		} else {
			CategoryDAO.addCategory(this.category = new Category(name));
			retInfo = "消费类别添加成功";
			success = true;
		}

		if (success) {
			JCMessageBox.createInformationMessageBox(this, getTitle(), retInfo)
					.open();
			dispose();
		} else {
			JCMessageBox.createErrorMessageBox(this, getTitle(), retInfo)
					.open();
			this.txtName.requestFocus();
		}
	}

	private void cancel() {
		this.category = null;
		dispose();
	}

	public Category getCategory() {
		return this.category;
	}

	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();

		if (source == this.btnOk) {
			submit();
		} else if (source == this.btnCancel) {
			cancel();
		}
	}
}